//
//  CommonViewController.m
//  danpin
//
//  Created by chuangjia on 28/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "CommonViewController.h"
#import "ZShare.h"
#import "UserRegisterNameVC.h"
#import "UserNavigationController.h"
#import "UINavigationController+YRBackGesture.h"
#import "SDImageCache.h"
@interface CommonViewController ()<UserDelegate>
{
    
}
@property(nonatomic,strong) UIButton *backButton;
@property(nonatomic,strong) UIButton *likeButton;
@property(nonatomic,strong) UIButton *shaidanBtn;
@property(nonatomic,strong) UIButton *shareButton;
@property(nonatomic,strong) UIButton *indexShaidanButton;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UIButton *rightButton;
@end

@implementation CommonViewController

@synthesize isIndex=_isIndex;
@synthesize isComment=_isComment;
@synthesize isDetail=_isDetail;
@synthesize isShaidan=_isShaidan;
@synthesize isIndexShaidan=_isIndexShaidan;
@synthesize isGood=_isGood;
@synthesize backButton=_backButton;
@synthesize likeButton=_likeButton;
@synthesize shaidanBtn=_shaidanBtn;
@synthesize shareButton=_shareButton;
@synthesize indexShaidanButton=_indexShaidanButton;
@synthesize rightButton=_rightButton;
@synthesize navHeight=_navHeight;
@synthesize navTitle=_navTitle;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];//UIColorFromRGB(0xf5f5f5);
    self.navigationController.navigationBarHidden=YES;
    [self initNavView];
}
-(void)dealloc
{
    self.backButton=nil;
    self.likeButton=nil;
    self.shaidanBtn=nil;
    self.shareButton=nil;
    self.indexShaidanButton=nil;
    self.rightButton =nil;
    self.titleLabel=nil;
    self.navTitle=nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
     [[SDImageCache sharedImageCache] clearMemory];
}

-(BOOL)isLogin
{
    if ([[ZShare sharedInstance] isLogin]) {
        return YES;
    }else
    {
        UserNavigationController *viewcontroller =[UserNavigationController newWithCameraDelegate:self];
        [self presentViewController:viewcontroller animated:NO completion:nil];
        
        return NO;
    }
    return NO;
}
-(void)userLoginOK:(id)userinfo
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)userDidCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)setIsShaidan:(BOOL)isShaidan
{
    _isShaidan=isShaidan;
    if (_isShaidan) {
        _shaidanBtn.hidden = NO;
        [_shaidanBtn setTitle:@"补充" forState:UIControlStateNormal];
    }else{
        _shaidanBtn.hidden = YES;
        
    }
}
-(void)setIsGood:(BOOL)isGood
{
    _isGood=isGood;
    if (_isGood) {
        _shaidanBtn.hidden = NO;
        [_shaidanBtn setTitle:@"晒单" forState:UIControlStateNormal];
    }else{
        _shaidanBtn.hidden = YES;
    }
}
-(void)setIsIndex:(BOOL)isIndex
{
    _isIndex=isIndex;
    _backButton.hidden =YES;
    _shaidanBtn.hidden =YES;
    _likeButton.hidden =YES;
    _shareButton.hidden =YES;
    
    _rightButton.hidden = YES;
    _indexShaidanButton.hidden=YES;
}
-(void)setIsDetail:(BOOL)isDetail
{
    _isDetail=isDetail;
    _backButton.hidden =NO;
    _likeButton.hidden =NO;
    _shareButton.hidden =NO;
  
    _indexShaidanButton.hidden=YES;
    _rightButton.hidden = YES;
}
-(void)setIsComment:(BOOL)isComment
{
    _isComment=isComment;
    _backButton.hidden =NO;
    _shaidanBtn.hidden =YES;
    _likeButton.hidden =YES;
    _shareButton.hidden =YES;
 
    _rightButton.hidden = YES;
    _indexShaidanButton.hidden=YES;
}
-(void)setNavTitle:(NSString *)navTitle
{
    _navTitle=navTitle;
    _titleLabel.text = navTitle;
}

-(void)setIsIndexShaidan:(BOOL)isIndexShaidan
{
    _isIndexShaidan = isIndexShaidan;
    _backButton.hidden =YES;
    _shaidanBtn.hidden =YES;
    _likeButton.hidden =YES;
    _shareButton.hidden =YES;
 
    _indexShaidanButton.hidden=NO;
     _rightButton.hidden = YES;
}
-(void)setRightTitle:(NSString *)rightTitle
{
    _rightTitle=rightTitle;
    _backButton.hidden =NO;
    _shaidanBtn.hidden =YES;
    _likeButton.hidden =YES;
    _shareButton.hidden =YES;
   
    _indexShaidanButton.hidden=YES;
    
    _rightButton.hidden = NO;
    [_rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
-(CGFloat)navHeight
{
    return hightAuto(65);
}
-(void)initNavView
{
    UIView *headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(65))];
    headerView.backgroundColor=[UIColor clearColor];
    UIImageView *backView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, hightAuto(65))];
    backView.image =[UIImage imageNamed:@"titile_bg.png"];
    [headerView addSubview:backView];
    backView=nil;
    
    _titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 20+(hightAuto(65)-20-20)/2, self.view.frame.size.width, 20)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = UIColorFromRGB(0x808080);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = FONT(16);
    [headerView addSubview:_titleLabel];
    //
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(0, 20+(hightAuto(65)-20-25)/2, 30, 25);
    _backButton.tag =0;
    _backButton.hidden = YES;
    [_backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_backButton];
    //
    //
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(DEVICE_WIDTH -50- PHOTO_FRAME_WIDTH, 20+(hightAuto(65)-20-25)/2, 50, 25);
    _rightButton.tag =1;
    _rightButton.hidden=YES;
    _rightButton.titleLabel.font =FONT(14);
    [_rightButton setTitleColor:UIColorFromRGB(0x285790) forState:UIControlStateNormal];
    [_rightButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_rightButton];
    //分享
    _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _shareButton.frame = CGRectMake(DEVICE_WIDTH -56- PHOTO_FRAME_WIDTH, 20+(hightAuto(65)-20-30)/2, 56, 30);
    _shareButton.tag =2;
    _shareButton.hidden=YES;
    _shareButton.titleLabel.font=FONT(13);
    [_shareButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_shareButton setImage:[UIImage imageNamed:@"title_share"] forState:UIControlStateNormal];
    [_shareButton setTitle:@" 分享" forState:UIControlStateNormal];
    [_shareButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_shareButton];
    
    //求购
    _indexShaidanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _indexShaidanButton.frame = CGRectMake(DEVICE_WIDTH -30- PHOTO_FRAME_WIDTH, 20+(hightAuto(65)-20-35)/2, 30, 30);
    _indexShaidanButton.tag =6;
    _indexShaidanButton.hidden=YES;
    _indexShaidanButton.titleLabel.font =FONT(20);
    [_indexShaidanButton setTitleColor:UIColorFromRGB(0x285790) forState:UIControlStateNormal];
    [_indexShaidanButton setTitle:@"+" forState:UIControlStateNormal];
    [_indexShaidanButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_indexShaidanButton];

    //喜欢
    _shaidanBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    _shaidanBtn.frame = CGRectMake(_shareButton.frame.origin.x - 56 - PHOTO_FRAME_WIDTH, 20+(hightAuto(65)-20-30)/2, 56, 30);
    _shaidanBtn.tag =4;
    _shaidanBtn.hidden = YES;
    _shaidanBtn.titleLabel.font=FONT(13);
    [_shaidanBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_shaidanBtn setImage:[UIImage imageNamed:@"title_shai.png"] forState:UIControlStateNormal];
    [_shaidanBtn addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:_shaidanBtn];
    
    [self.view addSubview:headerView];
    headerView=nil;
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        default:
            break;
    }
}
+(UIView *)footerView:(NSString*)msg
{
    if (msg==nil || msg.length==0) {
        msg=@"单品 - 以一顶百";
    }
    UIView *footerview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 80)];
    footerview.backgroundColor=[UIColor clearColor];
    UILabel *footLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 30, [UIScreen mainScreen].bounds.size.width, 20)];
    footLabel.backgroundColor = [UIColor clearColor];
    footLabel.textColor = UIColorFromRGB(0xcccccc);
    footLabel.textAlignment = NSTextAlignmentCenter;
    footLabel.font = FONT(14);
    footLabel.text =msg;
    [footerview addSubview:footLabel];
    return footerview;
}
@end
